$(function () {

        var compensation_datagrid = $("#compensation_datagrid");
        var compensation_dialog = $("#compensation_dialog");
        var compensation_form = $("#compensation_form");
        var compensationDetails_dialog = $("#compensationDetails_dialog");
        var compensationDetails_form = $("#compensationDetails_form");
        var showdetails_dialog = $("#showdetails_dialog");
        var showdetails_form = $("#showdetails_form");


        var object = {
            addDetail: function () {
                var row = compensation_datagrid.datagrid('getSelected');
                if (row == null) {
                    $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                } else {
                    if (row.totalFee != null) {
                        $.messager.alert('温馨提示!', "此保单赔偿计算已完成, 不能再修改", 'error');
                        return;
                    }
                    else {
                        compensationDetails_dialog.dialog('setTitle', '理赔计算');
                        compensationDetails_form.form('clear');
                        $('#sn').textbox('setValue', row.sn);
                        //compensationDetails_form.form('load', row.sn);
                        compensationDetails_dialog.dialog('open');
                    }
                }
            },

            edit: function () {
                var row = compensation_datagrid.datagrid('getSelected');
                if (row == null) {
                    $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                } else {
                    var row = compensation_datagrid.datagrid('getSelected');

                    if (row.totalFee != null && row.receiverName != null && row.bankAccount != null && row.bankAddress != null && row.alipay != null &&
                        row.carNum != null && row.telNum != null && row.email != null) {
                        $.messager.alert('温馨提示!', "信息已录入, 不能再修改", 'error');
                        return;
                    }
                    else {
                        compensation_form.form('clear');
                        compensation_form.form('load', row);
                        compensation_dialog.dialog('setTitle', '赔损人信息补全');
                        compensation_dialog.dialog('open');
                    }
                }
            },

            //查看赔款明细
            lookDetail: function () {
                var row = compensation_datagrid.datagrid('getSelected');
                //alert(row.totalFee);
                if (row == null) {
                    $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                } else {
                    if (row.totalFee == null) {
                        $.messager.alert('温馨提示', '赔款明细还未核算!!', 'info');
                        return;
                    }
                    else {
                        showdetails_form.form('load', $.get("/compensationDetails/lookDetails.do", {sn: row.sn}, function (data) {
                            if (row.sn != data.sn) {
                                $.messager.alert('提示!', '没有查询到此数据, 数据无效!', 'warning');
                                return;
                            } else {
                                $('#showsn').textbox('setValue', data.sn);
                                $('#showinsuranceKind').textbox('setValue', data.insuranceKind);
                                $('#showinsuranceResp').textbox('setValue', data.insuranceResp);
                                $('#showinsuranceMax').textbox('setValue', data.insuranceMax);
                                $('#showinsuranceRate').textbox('setValue', data.insuranceRate);
                                $('#showcarlossFee').textbox('setValue', data.carlossFee);
                                $('#showglassFee').textbox('setValue', data.glassFee);
                                $('#showfireFee').textbox('setValue', data.fireFee);
                                $('#showfixFee').textbox('setValue', data.fixFee);
                                $('#showdeadFee').textbox('setValue', data.deadFee);
                                $('#showdisabledFee').textbox('setValue', data.disabledFee);
                                $('#showmedicalFee').textbox('setValue', data.medicalFee);
                                $('#showhospitalFee').textbox('setValue', data.hospitalFee);
                                $('#showtotalFee').textbox('setValue', data.totalFee);
                                showdetails_dialog.dialog('setTitle', '保单赔款明细');
                                showdetails_dialog.dialog('open');
                            }
                        })
                        )
                    }
                }
            },

            delete: function () {
                var row = compensation_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert('提示!', '你没有选中任何数据!', 'warning');
                    return;
                }
                $.messager.confirm('确认', '确定要删除这条数据吗？', function (r) {
                    if (r) {
                        $.get("/compensation/delete.do", {id: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('温馨提示!', "删除成功", 'info', function () {
                                    compensation_datagrid.datagrid("reload");
                                });
                            }
                        });
                    }
                    else {
                        $.messager.alert('温馨提示!', "删除失败", 'error');
                        return;
                    }
                });
            },
            compensatiosave: function () {
                compensation_form.form("submit", {
                        url: "/compensation/saveOrUpdate.do",
                        success: function (data) {
                            var data = $.parseJSON(data);
                            if (data.success) {
                                $.messager.alert('温馨提示', "保存成功", 'info', function () {
                                    compensation_dialog.dialog('close');
                                    compensation_datagrid.datagrid('reload');
                                });
                            } else {
                                $.messager.alert('温馨提示', "保存失败", 'error');
                            }
                        }
                    }
                );
            },

            compensationcancel: function () {
                compensation_dialog.dialog('close');
            },
            detailscancel: function () {
                compensationDetails_dialog.dialog('close');
            },
            showdetailscancel: function () {
                showdetails_dialog.dialog('close');
            },
            reload: function () {
                compensation_datagrid.datagrid('reload');
            },
            //计算赔付金额, 验证格式
            detailcaculate: function () {
                var carloss = (isNaN($("#carlossFee").textbox('getValue')));
                var glassloss = (isNaN($("#glassFee").textbox('getValue')));
                var fireloss = (isNaN($("#fireFee").textbox('getValue')));
                var fixloss = (isNaN($("#fixFee").textbox('getValue')));
                var deadloss = (isNaN($("#deadFee").textbox('getValue')));
                var disabledloss = (isNaN($("#disabledFee").textbox('getValue')));
                var medicalloss = (isNaN($("#medicalFee").textbox('getValue')));
                var hospitalloss = (isNaN($("#hospitalFee").textbox('getValue')));

                $.messager.alert('注意', '请保证数据的真实性和正确性!','info');

                if (carloss || glassloss || fireloss || deadloss || disabledloss || medicalloss || hospitalloss) {
                    $.messager.alert('温馨提示!', "输入的格式不正确 或 没有选择'投保类型 和 有无责任!", 'error');
                    return
                }
                else {
                    var insuranceRate = parseFloat($("#insuranceRate").textbox('getValue')) || 0;
                    if (isNaN(insuranceRate)) {
                        $.messager.alert('温馨提示!', "赔付率不能小于零", 'error');
                        $.messager.confirm('注意', '请保证数据的真实性和正确性!');
                    }
                    else if (insuranceRate > 1) {
                        $.messager.alert('温馨提示!', "赔付率不能大于100%", 'error');
                    }
                    else {
                        var carlossFee = $("#carlossFee").textbox('getValue') || 0;
                        var glassFee = $("#glassFee").textbox('getValue') || 0;
                        var fireFee = $("#fireFee").textbox('getValue') || 0;
                        var fixFee = $("#fixFee").textbox('getValue') || 0;
                        var deadFee = $("#deadFee").textbox('getValue') || 0;
                        var disabledFee = $("#disabledFee").textbox('getValue') || 0;
                        var medicalFee = $("#medicalFee").textbox('getValue') || 0;
                        var hospitalFee = $("#hospitalFee").textbox('getValue') || 0;

                        //var total = carlossFee + glassFee + fireFee + fixFee + deadFee + disabledFee + medicalFee + hospitalFee;
                        var total = parseFloat(carlossFee) + parseFloat(glassFee) + parseFloat(fireFee) + parseFloat(fixFee) +
                            parseFloat(deadFee) + parseFloat(disabledFee) + parseFloat(medicalFee) + parseFloat(hospitalFee);

                        var totalFee = $("#totalFee").textbox('getValue');
                        totalFee = Math.round((total * insuranceRate) * 1000) / 1000;  //保留3位小数
                        $('#totalFee').textbox('setValue', totalFee);  //设置新值

                        //获取新值在判断
                        var totalfeecal = parseFloat($('#totalFee').textbox('getValue'));
                        var insuranceMax = parseFloat($('#insuranceMax').textbox('getValue'));
                        if (isNaN(insuranceMax)) {
                            $.messager.alert('温馨提示!', "您未选择[最高赔付额]", 'error');
                            $('#totalFee').textbox('setValue', '');
                        } else if (totalfeecal > insuranceMax) {
                            $.messager.alert('重要提示!', "计算赔付额不能大于最大赔付额!", 'error');
                        }
                        else {
                            $.messager.confirm('确认', '计算完成!');
                        }
                    }
                }
            },

            //保存赔偿明细
            detailsave: function () {
                $.messager.confirm('重要提示!', '请确保所有数据的完整性和有效性, 保存后不可更改!', function (r) {
                    var row = compensation_datagrid.datagrid('getSelected');
                    sn = row.sn;
                    var totalfeecal = parseFloat($('#totalFee').textbox('getValue'));
                    var insuranceMax = parseFloat($('#insuranceMax').textbox('getValue'));
                    if (totalfeecal > insuranceMax) {
                        $.messager.alert('重要提示!', "计算赔付额不能大于最大赔付额!", 'error');
                    }
                    else {
                        compensationDetails_form.form("submit", {
                                url: "/compensationDetails/saveOrUpdate.do",
                                success: function (data) {
                                    var data = $.parseJSON(data);
                                    if (data.success) {
                                        $.messager.alert('温馨提示', "保存成功", 'info', function () {
                                            compensationDetails_dialog.dialog('close');
                                            compensation_datagrid.datagrid('reload');
                                        });
                                    } else {
                                        $.messager.alert('温馨提示', "保存失败", 'error');
                                    }
                                }
                            }
                        );
                    }
                })
            },

            searchInfo: function () {
                var keyword = $("#keyword").textbox('getValue');
                compensation_datagrid.datagrid("load", {
                    keyword: keyword
                });
            }
        }

        /*事件统一绑定*/
        $("[data-cmd]").click(function () {
            var cmd = $(this).data('cmd');
            object[cmd]();
        })

        compensation_datagrid.datagrid({
            fit: true,
            url: "/compensation/query.do",
            rownumbers: true,
            fitColumns: true,
            pagination: true,
            singleSelect: true,
            toolbar: '#compensation_toolbar',
            columns: [[
                {field: 'sn', title: '保单号', width: 100, align: 'center'},
                {field: 'receiverName', title: '报案人姓名', width: 100, align: 'center'},
                {field: 'idNum', title: '身份证号', width: 100, align: 'center'},
                {field: 'totalFee', title: '赔款总额(元)', width: 100, align: 'center'},
                {field: 'bankAccount', title: '银行卡号', width: 100, align: 'center'},
                {field: 'bankAddress', title: '开户行', width: 100, align: 'center'},
                {field: 'alipay', title: '支付宝账号', width: 100, align: 'center'},
                {field: 'carNum', title: '车牌号', width: 100, align: 'center'},
                {field: 'telNum', title: '联系电话', width: 100, align: 'center'},
                {field: 'email', title: '邮箱', width: 100, align: 'center'},
                {field: 'remark', title: '备注', width: 100, align: 'center'},
            ]]
        });

        compensation_dialog.dialog({
            closed: true,
            width: 700,
            height: 350,
            buttons: $("#compensation_diaButtons")
        });


        compensationDetails_dialog.dialog({
            closed: true,
            width: 600,
            height: 450,
            buttons: $("#compensationDetails_diaButtons")
        });

        showdetails_dialog.dialog({
            closed: true,
            width: 600,
            height: 450,
            buttons: $("#showdetails_Buttons")
        });


    }
)

